/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {Text, View, Image, TouchableHighlight, WebView, Platform, NativeModules, ListView} from 'react-native';

import Common from '../../component/common';

import Loading from '../../component/loading';

import styles from '../../style/media';

import Article from './article';

import MoreIndustryMedia from './moreIndustryMedia';

const wechat = require('../../images/wechat.png');
const weibo = require('../../images/weibo.png');
const web = require('../../images/newspaper.png');

export default class IndustryMediaList extends Component {
  constructor(props) {
    super(props);
    var dsindustry = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.state = {
      loading: false,
      isMore: true,
      industryMediaDataSource: dsindustry,
    };
    this.industrymedia = [];
    this.industrymediamore = [];
    this.industrymediatotal = [];
  }
  componentWillMount(){
    if(this.props.data.mmNewsList){
      this.industrymedia = this.props.data.mmNewsList;
      this.setState({
        industryMediaDataSource: this.state.industryMediaDataSource.cloneWithRows(this.industrymedia),
      });
    }
  }
  industryMediaMore = () => {
    this.setState({loading: true});
    this.timer = setTimeout(() => {
      if(!this.industrymediamore.length){
        Common.fetchMore({id: this.props.data.newsGroupId || this.props.data.programId, type: this.props.data.type,newsType: 1},(responseData) => {
          // console.log(responseData.resultList);
          this.industrymediamore = responseData.resultList;
          this.industrymediatotal = this.industrymedia.concat(this.industrymediamore);
          this.setState({
            loading: false,
            isMore: false,
            industryMediaDataSource: this.state.industryMediaDataSource.cloneWithRows(this.industrymediatotal)});
        },() => {
          console.log("error");
        }, () => {
          console.log("error link");
        });
      }else{
        this.industrymediatotal = this.industrymedia.concat(this.industrymediamore);
        this.setState({
            loading: false,
            isMore: false,
            industryMediaDataSource: this.state.industryMediaDataSource.cloneWithRows(this.industrymediatotal)});
      }
      clearTimeout(this.timer);
    }, 500);
  };
  industryMediaRestore = () => {
    this.setState({
      isMore: true,
      industryMediaDataSource: this.state.industryMediaDataSource.cloneWithRows(this.industrymedia)});
  };
  linking = (val) => {
    if(Platform.OS == 'ios'){
      NativeModules.JsAndroid.openUrl(val.url);
    }else{
      Common.onForward(this, Article, val, 5);
    }
  }
  renderRow = (val) => {
    let sourceImage ;
    switch(val.scrabbleType){
      case 'wechat':
        sourceImage = wechat;
        break;
      case 'web':
        sourceImage = web;
        break;
      default :
        sourceImage = weibo;
        break;
    }
    return (
      <TouchableHighlight style={styles.reportListItems} underlayColor="#fafafa" delayPressIn={100} onPress={() => {this.linking(val)}}>
        <View>
          <View style={styles.reportListIW}>
            <Text style={styles.reportListIT} numberOfLines={1}>{val.title}</Text>
          </View>
          <View style={styles.reportLIs}>
            <View style={styles.reportLIsr}>
              <Image style={styles.reportLIsri} source={sourceImage} />
              <Text style={styles.reportLIsrl}>{val.company}</Text>
            </View>
            <View style={styles.reportLIsr}>
              <Image style={styles.reportLIsri} source={require('../../images/timer.png')} />
              <Text style={styles.reportLIsrl}>
              {
                Common.beforeTime(val.createTime,5)
              }
              </Text>
            </View>
            {/*<View style={styles.reportLIsr}>
              <Text style={styles.reportLIsrc}>{val.sourceDegree}</Text>
              <Text style={styles.reportLIsrl}>阅读</Text>
            </View>*/}
          </View>
        </View>
      </TouchableHighlight>
    )
  };
  render() {
    return (
        <View>
          {this.industrymedia?
            <ListView
              scrollEnabled={true}
              removeClippedSubviews={false}
              dataSource={this.state.industryMediaDataSource}
              renderRow={this.renderRow}
              enableEmptySections={true}
            />:null
          }
          {this.props.data.isMmNewsListMore?
            <TouchableHighlight underlayColor="#fafafa" delayPressIn={100} disabled={this.state.loading} onPress={() => Common.onForward(this,MoreIndustryMedia,"history",this.props.data)}>
              <View style={styles.reportMore} ><Text style={styles.reportMoreT}>查看全部</Text><Image style={styles.reportMoreI} source={require('../../images/specialColumn/xia.png')} /></View>
            </TouchableHighlight>:null
          }
        </View>
    )
  }
}